package com.car.job;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.car.action.admin.CardController;
import com.car.common.model.CardDiscuss;
import com.car.common.model.MessageUnreadNum;
import com.car.common.model.TempWrap;
import com.car.common.model.UserFans;
import com.car.common.model.UserSees;
import com.car.common.model.Users;
import com.car.tx.UserWrap;
import com.car.tx.UserWrap2;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.utils.RongCloudUtil;
import com.utils.StringUtil;
import com.utils.SystemClock;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * Created by chengjun on 2019/2/28.
 */
public class QuartzJobOne implements Job {
	
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
    	Db.update("update  users as u LEFT JOIN temp_wrap as t on t.user_id = u.id set u.user_mobile = t.user_mobile where u.user_mobile is null and t.id > 0");
    	Db.delete("delete t from temp_wrap as t LEFT JOIN users as u on t.user_id = u.id  where  u.user_mobile is not null and t.id > 0");
    	
    	if (new Random().nextInt(20) == 1) {
    		Users girlOne = Users.dao.findFirst("select id ,area_id from users where sex = 2 and is_true = 1 and up_time > "+ (SystemClock.now()/1000L - 3*24*3600L) +" ORDER BY RAND() LIMIT 1");
        	
        	String sql = "select id  from users where sex = 1 and is_true > 1 and area_id = "+girlOne.getAreaId()+" ORDER BY RAND() LIMIT 1 ";
    	    Users boyOne = Users.dao.findFirst(sql);
        	int user_id = boyOne.getId().intValue(); 
        	int target_user_id = girlOne.getId().intValue();
        	UserSees one1 = new UserSees();
    		one1.setUserId(user_id);
    		one1.setTargetUserId(target_user_id);
    		one1.setCreateTime(SystemClock.now()/1000L - 1 * 60);
    		one1.setUpdateTime(SystemClock.now()/1000L);
    		if (one1.save()) {
    			new CardController().giveABPoint(user_id);
    			MessageUnreadNum.dao.addNum(MessageUnreadNum.MESSAGE_UNREAD_TYPE_SEE, target_user_id);
    		}
        	//判断是否关注
    		UserFans one = UserFans.dao.findFirst("select id , user_id from user_fans where user_id = " + user_id+ " and target_user_id = " + target_user_id +" and delete_time is null ");
    		if (one == null) {
    			one = new UserFans();
    			one.setUserId(user_id);
    			one.setTargetUserId(target_user_id);
    			one.setIsAuth(1);
    			one.setStatus(1);
    			one.setCreateTime(SystemClock.now()/1000L);
    			one.setUpdateTime(SystemClock.now()/1000L);
    			if (one.save()) {
    				new CardController().giveABPoint(user_id);
    				MessageUnreadNum.dao.addNum(MessageUnreadNum.MESSAGE_UNREAD_TYPE_FAN, target_user_id);
    			}	
    		} 
    	}
    	
	    
    	/*
		PropKit.use("config.properties");
        // 注册 JDBC 驱动
		Connection  conn = StringUtil.getConn();
		//  delete t from temp_wrap as t LEFT JOIN users as u on t.user_id = u.id  where  u.user_mobile is not null and t.id > 0
        List<TempWrap> all = TempWrap.dao.find("select * from temp_wrap ");
        for(TempWrap one :all) {
        	Users users = Users.dao.findById(one.get("user_id"));
        	if (StringUtil.empty(one.getStr("user_password"))) {
        		if (one.getStr("user_mobile").equals(users.getUserMobile())) {
            		one.delete();
            	} else {
            		try {
            			new UserWrap(one.getStr("user_mobile"),String.valueOf(one.get("user_id"))).call(conn);
            			RongCloudUtil.sendTxt("141", String.valueOf(one.get("user_id")), "用户必读：因部分用户发布含有不良信息的动态，即日起用户发布动态实行审核制，用户动态发布后需要经过审核通过后才能展示。\r\n" + 
            					"            					 禁止发布以下内容： \r\n" + 
            					"            					 1.任何违反法律法规，涉及国家政治及军事的图片或内容； \r\n" + 
            					"            					 2.任何淫秽、色情、低俗的图片或内容（有明显的敏感部分裸露的图片，描述性交行为的体位或姿势和色情场景的图片，包括后期打码遮盖）；\r\n" + 
            					"            					 3.任何涉及未成年的图片或内容； \r\n" + 
            					"            					 4.任何涉及有偿、红包、金钱交易等的图片或内容； \r\n" + 
            					"            					 5.违规交友（找主、找M、找狗等低俗语言描述的交友），请使用APP底部的“交友”功能；\r\n" + 
            					"            					 6.任何未经允许宣传广告、群、推广以及二维码（包括QQ、微信）的图片或内容； \r\n" + 
            					"            					 7.骚扰其他用户，辱骂他人等其他不文明的内容； \r\n" + 
            					"            					 8.其他涉及不健康内容，可能造成不积极影响的言论；\r\n" + 
            					"            					 以上内容一经发现立即删除，对多次违反规定的用户做封号处理。");
            		} catch (Exception e) {
            			e.printStackTrace();
            		}
            	}
        	} else {
        		
        		if (one.getStr("user_mobile").equals(users.getUserMobile())) {
            		one.delete();
            	} else {
            		try {
            			new UserWrap2(one.getStr("user_mobile"),one.getStr("user_password"),String.valueOf(one.get("user_id"))).call(conn);
            			RongCloudUtil.sendTxt("141", String.valueOf(one.get("user_id")), "用户必读：因部分用户发布含有不良信息的动态，即日起用户发布动态实行审核制，用户动态发布后需要经过审核通过后才能展示。\r\n" + 
            					"            					 禁止发布以下内容： \r\n" + 
            					"            					 1.任何违反法律法规，涉及国家政治及军事的图片或内容； \r\n" + 
            					"            					 2.任何淫秽、色情、低俗的图片或内容（有明显的敏感部分裸露的图片，描述性交行为的体位或姿势和色情场景的图片，包括后期打码遮盖）；\r\n" + 
            					"            					 3.任何涉及未成年的图片或内容； \r\n" + 
            					"            					 4.任何涉及有偿、红包、金钱交易等的图片或内容； \r\n" + 
            					"            					 5.违规交友（找主、找M、找狗等低俗语言描述的交友），请使用APP底部的“交友”功能；\r\n" + 
            					"            					 6.任何未经允许宣传广告、群、推广以及二维码（包括QQ、微信）的图片或内容； \r\n" + 
            					"            					 7.骚扰其他用户，辱骂他人等其他不文明的内容； \r\n" + 
            					"            					 8.其他涉及不健康内容，可能造成不积极影响的言论；\r\n" + 
            					"            					 以上内容一经发现立即删除，对多次违反规定的用户做封号处理。");
            		} catch (Exception e) {
            			e.printStackTrace();
            		}
            	}
        	}
		
       }
       /* 
       List<CardDiscuss> discussList = CardDiscuss.dao.find("select d.* from card_discuss as d LEFT JOIN  card as c on d.card_id = c.id where c.id is NULL ");
       for(CardDiscuss one :discussList) {
    	   Db.delete("delete from card_discuss where id =" + one.getId());
       }
       Db.delete("delete from user_sees where user_id = 0 ");
       
       Db.delete("DELETE l FROM card_like AS l LEFT JOIN users AS u ON u.id = l.user_id WHERE u.is_true = 2 AND (u.user_header IS NULL or LENGTH(u.user_header) =0)");
       
       Db.delete("DELETE l FROM user_sees AS l LEFT JOIN users AS u ON u.id = l.user_id WHERE u.is_true = 2 AND (u.user_header IS NULL or LENGTH(u.user_header) =0)");
       
       Db.delete("DELETE l FROM card_discuss AS l LEFT JOIN users AS u ON u.id = l.user_id WHERE u.is_true = 2 AND (u.user_header IS NULL or LENGTH(u.user_header) =0)");
       */
    }
}
